/**
 * 
 */
package com.yidin.lsc;

import java.util.HashMap;
import java.util.Map;

import com.yidin.logic.MainService;
import com.yidin.logic.Task;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author cola
 * 
 */
public class RegisterView extends Views {
	private Activity activity;
	private View contentView;
	private Button first;
	private Button second;
	private Button login;
	private Button register;
	private TextView losePassword;
	private Button resetbtn;
	private ImageView photoBtn;
	private Spinner sexSpinner;
	private String AccountId;
	private String Password = "";
	private String Email = "";
	private String Membername = "";
	private Button loginBtn;
	private String Rphone = "";
	private String userSex = "1";
	/**
	 * 0 表示accountid
		1 表示 手机号码
		2 表示邮箱地址
	 */
	private String AccountType = "1";
	/**
	 * 0 pc
		1 i-os
		2 安卓
	 */
	private String DeviceInfo = "2";

	public RegisterView(Activity activity) {
		super(activity);
		// TODO Auto-generated constructor stub
		this.activity = activity;
		this.initialize();
	}

	@Override
	public void checkCache() {
		// TODO Auto-generated method stub

	}

	@Override
	public void findView() {
		// TODO Auto-generated method stub

	}

	private OnClickListener resetOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Map obj = new HashMap<String, String>();
			obj.put("Email", resetEdit.getText().toString());

			((Register) activity).showProgress(activity);
			Task task = new Task(Task.TASK_RESET_PASSWORD, obj);
			MainService.AddNewTask(task);
			contentView = addView(R.layout.login_activity);
			// register = (Button) contentView.findViewById(R.id.loginRegister);
			// register.setOnClickListener(registerOnClickListener);
			// losePassword = (TextView)
			// contentView.findViewById(R.id.losePassword);
			// losePassword.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线
			// losePassword.setOnClickListener(loseOnClickListener);
			initialize();
		}

	};

	private EditText resetEdit;
	private OnClickListener loseOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			contentView = addView(R.layout.resetpassword);
			resetbtn = (Button) contentView.findViewById(R.id.resetbtn);
			resetbtn.setOnClickListener(resetOnClickListener);
			resetEdit = (EditText) contentView
					.findViewById(R.id.reset_in);
		}

	};

	private OnClickListener registerOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			contentView = addView(R.layout.register);
			first = (Button) contentView.findViewById(R.id.first);
			first.setOnClickListener(firstOnClickListener);
		}

	};

	private String[] sex = new String[] { "男", "女" };
	/**
	 * 手机或邮箱，密码输入界面
	 */
	private OnClickListener firstOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			int len = 0;
			boolean phone = false;
			AccountId = ((TextView) activity
					.findViewById(R.id.appid_account_in)).getText().toString();
			if (AccountId.indexOf("@") != -1 && AccountId.indexOf(".com") != -1) {
				len = 8;
				Email = AccountId;
				AccountType = "2";
			} else {
				len = 11;
				AccountType = "1";
				Rphone = AccountId;
				phone = true;
			}

			if (AccountId.equals("") || AccountId.length() < len) {
				if (phone && AccountId.length() != 11)
					Toast.makeText(activity, "电话号码必须为11位!!", Toast.LENGTH_LONG)
							.show();
				else
					Toast.makeText(activity, "邮箱或电话不能为空或邮箱长度不能少于3位!!",
							Toast.LENGTH_LONG).show();
				return;
			}

			Password = ((TextView) activity
					.findViewById(R.id.appid_password_in)).getText().toString();
			String passwordTwo = ((TextView) activity
					.findViewById(R.id.appid_passwordtwo_in)).getText()
					.toString();
			if (Password.equals("") || passwordTwo.equals("")
					|| !Password.equals(passwordTwo)) {
				Toast.makeText(activity, "密码不能为空或两个密码不能不相同!!",
						Toast.LENGTH_LONG).show();
				return;
			}

			if (Password.length() < 5) {
				Toast.makeText(activity, "密码长度不能少于5位 !!", Toast.LENGTH_LONG)
						.show();
				return;
			}
			contentView = addView(R.layout.register2);
			second = (Button) activity.findViewById(R.id.second);
			second.setOnClickListener(secondOnClickListener);
			sexSpinner = (Spinner) activity.findViewById(R.id.spinnerSex);
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
					android.R.layout.simple_spinner_item, sex);
			// 设置下拉列表的风格
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			sexSpinner.setAdapter(adapter);
			sexSpinner.setOnItemSelectedListener(itemSelect);
		}

	};

	private OnClickListener secondOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Membername = ((TextView) activity.findViewById(R.id.appid_name_in))
					.getText().toString();
			if (Membername.equals("") || Membername.length() < 2) {
				Toast.makeText(activity, "名字不能少于二个字!!", Toast.LENGTH_LONG)
						.show();
				return;
			}
			contentView = addView(R.layout.register3);
			second = (Button) activity.findViewById(R.id.third);
			second.setOnClickListener(thirdOnClickListener);
			photoBtn = (ImageView) contentView.findViewById(R.id.userFace);
			((Button) activity.findViewById(R.id.photo))
					.setOnClickListener(photoClickListener);
			((Button) activity.findViewById(R.id.photograph))
					.setOnClickListener(photographClickListener);
		}

	};

	private OnItemSelectedListener itemSelect = new OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
			// TODO Auto-generated method stub
			if (parent.getItemAtPosition(position).toString().equals("男"))
				userSex = "1";
			else
				userSex = "0";
		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {
			// TODO Auto-generated method stub

		}
	};

	private OnClickListener photographClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent getImageByCamera = new Intent(
					"android.media.action.IMAGE_CAPTURE");
			activity.startActivityForResult(getImageByCamera, 1);
		}

	};

	private OnClickListener photoClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
			getImage.addCategory(Intent.CATEGORY_OPENABLE);
			getImage.setType("image/jpeg");
			activity.startActivityForResult(getImage, 0);
		}

	};

	private OnClickListener inOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Password = ((TextView) contentView
					.findViewById(R.id.loginIn_password_in)).getText()
					.toString();
			AccountId = ((TextView) contentView.findViewById(R.id.login_in))
					.getText().toString();
			Map obj = new HashMap<String, String>();
			obj.put("AccountId", AccountId);
			obj.put("Password", Password);
			obj.put("AccountType", AccountType);
			obj.put("DeviceInfo", DeviceInfo);

			((Register) activity).showProgress(activity);
			Task task = new Task(Task.TASK_USER_LOGIN, obj);
			MainService.AddNewTask(task);
			// activity.setResult(0);
			// activity.finish();
		}

	};

	private OnClickListener thirdOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Map obj = new HashMap<String, String>();
			obj.put("AccountId", AccountId);
			obj.put("Password", Password);
			obj.put("Telephone", Rphone);
			obj.put("Sex", userSex);
			obj.put("Email", Email);
			obj.put("Membername", Membername);

			((Register) activity).showProgress(activity);
			Task task = new Task(Task.TASK_USER_REGISTER, obj);
			MainService.AddNewTask(task);
			// activity.finish();
		}

	};

	@Override
	public void showFunctionsListView() {
		// TODO Auto-generated method stub
		// contentView = addView(R.layout.register);
		contentView = addView(R.layout.login_activity);
	}

	@Override
	public void initialize() {
		// TODO Auto-generated method stub
		this.login = (Button) contentView.findViewById(R.id.loginLogin);
		this.login.setOnClickListener(inOnClickListener);
		register = (Button) contentView.findViewById(R.id.loginRegister);
		register.setOnClickListener(registerOnClickListener);
		this.losePassword = (TextView) contentView
				.findViewById(R.id.losePassword);
		this.losePassword.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);// 下划线
		this.losePassword.setOnClickListener(loseOnClickListener);
	}

	public void setImage(Bitmap bm) {
		this.photoBtn.setImageBitmap(bm);
	}

	public void registerResult(boolean login, String info) {
		if (login) {
			Toast.makeText(activity, "注册成功", Toast.LENGTH_LONG).show();
			activity.finish();
		} else if(info.equals(""))
			Toast.makeText(activity, "服务器返回失败信息", Toast.LENGTH_LONG).show();
		else
			Toast.makeText(activity, info, Toast.LENGTH_LONG).show();
	}
	
	public void resetPassword(boolean login, String info) {
		if (login) {
			Toast.makeText(activity, "重置邮件已发到你邮箱，请注意查收", Toast.LENGTH_LONG).show();
		} else if(info.equals(""))
			Toast.makeText(activity, "服务器返回失败信息", Toast.LENGTH_LONG).show();
		else
			Toast.makeText(activity, info, Toast.LENGTH_LONG).show();
	}

	@Override
	public void refresh(Object... param) {
		// TODO Auto-generated method stub
		
	}
}
